'use strict';
app.controller('rechargeConfigureCtrl', ['$scope', '$stateParams', '$modal', 'utilService', 'rechargeConfigureService', 'toaster', function ($scope, $stateParams, $modal, utilService, rechargeConfigureService, toaster) {

    $scope.rechargeConfigure = {};

    // call server.
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'recharge/configure/all',
            params: $scope.rechargeConfigure
        }).then(function (result) {
        });

    }

    $scope.query();


    $scope.del = function (id, rechargeMoney) {
        var param = {
            id: id,
            name: rechargeMoney
        };

        // query data.
        openModal("你是否要删除充值【" + rechargeMoney + "元】的赠送规则？");

        function openModal(msg) {
            utilService.modal($modal, $scope, {
                title: "删除赠送规则",
                controller: "rechargeConfigureDeleteCtrl",
                message: msg,
                data: param
            });
        }
    }


}]);


app.controller('rechargeConfigureAddCtrl', ['$scope', '$state', 'utilService', 'rechargeConfigureService', 'toaster', function ($scope, $state, utilService, rechargeConfigureService, toaster) {
    $scope.rechargeConfigureService = rechargeConfigureService;
    // call server.
    $scope.add = function () {
        rechargeConfigureService.add($scope).then(function (result) {

            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.rechargeConfigure');
                    break;
                case 716:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);


app.controller('rechargeConfigureEditCtrl', ['$scope', '$state', '$stateParams', 'utilService', 'rechargeConfigureService', 'toaster', function ($scope, $state, $stateParams, utilService, rechargeConfigureService, toaster) {
    $scope.id = $stateParams.id;
    $scope.rechargeConfigureService = rechargeConfigureService;
    $scope.rechargeConfigureService.rechargeConfigureEditVO.id = $scope.id;

    $scope.findById = function () {
        rechargeConfigureService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    rechargeConfigureService.rechargeConfigureEditVO = result.data.data;
                    break;
                case 675:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.findById();

    // call server.
    $scope.edit = function () {
        rechargeConfigureService.edit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.rechargeConfigure');
                    break;
                case 675:
                    toaster.pop('waiting', "提示", result.message);
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

app.controller('rechargeConfigureDeleteCtrl', ['$scope', '$modalInstance', '$state', 'rechargeConfigureService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, rechargeConfigureService, toaster, modalData) {
        $scope.modal = modalData;
        $scope.isDelModal = true;

        //确认删除
        $scope.ok = function () {
            if (!$scope.isDelModal) {
                return;
            }
            $scope.isDelModal = false;
            rechargeConfigureService.delete($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.query();
                        toaster.pop('success', "提示", "操作成功");;
                        break;
                    case 675:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal = true;
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);